package com.jason.game.online.home.view

import android.content.Context
import android.widget.LinearLayout
import com.cby.library.common.util.ScreenUtils
import com.cby.library.data.model.BannerBean
import com.cby.library.data.model.PosterBean

/**
 * @description: HomeHeaderView
 *
 * @author ChenBaoyang
 * @date 2021/10/27 16:20
 */
class HomeHeaderView(context: Context) : LinearLayout(context) {

    private var bannerViewSection = HomeBannerViewSection(context)
    private var posterViewSection = HomePosterViewSection(context)
    private var adViewSection = HomeAdViewSection(context)

    init {
        orientation = VERTICAL
        // 添加banner view
        addView(bannerViewSection.rootView, bannerLayoutParams)
        // 添加poster view
        addView(posterViewSection.rootView, posterLayoutParams)
        // 添加Ad view
        addView(adViewSection.rootView, adLayoutParams)
    }

    /**
     * banner view的布局参数
     */
    private val bannerLayoutParams: LayoutParams
        get() {
            val layoutParams = defaultLayoutParams
            val topPadding = ScreenUtils.dip2px(context, 7f)
            layoutParams.topMargin = topPadding
            return layoutParams
        }

    /**
     * poster view的布局参数
     */
    private val posterLayoutParams: LayoutParams
        get() {
            val params: LayoutParams = defaultLayoutParams
            val topPadding = ScreenUtils.dip2px(context, 12f)
            params.topMargin = topPadding
            return params
        }

    /**
     * ad view的布局参数
     */
    private val adLayoutParams: LayoutParams
        get() {
            val params: LayoutParams = defaultLayoutParams
            val space = ScreenUtils.dip2px(context, 20f)
            params.topMargin = space
            params.bottomMargin = ScreenUtils.dip2px(context, 14f)
            return params
        }

    /**
     * 当前通用默认的布局参数
     */
    private val defaultLayoutParams: LayoutParams
        get() {
        val layoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT
        )
        val padding = ScreenUtils.dip2px(context, 15f)
        layoutParams.leftMargin = padding
        layoutParams.rightMargin = padding
        return layoutParams
    }

    /**
     * 添加banner数据
     */
    fun setBanner(banner: List<BannerBean>?) {
        if (banner != null) {
            bannerViewSection.setData(banner)
        }
    }

    /**
     * 添加分类数据
     */
    fun setPoster(posters: List<PosterBean>?) {
        posters?.let {
            if (it.size > 5) {
                posterViewSection.setData(it.subList(0, 5))
            } else {
                posterViewSection.setData(it)
            }
        }
    }

    /**
     * 设置红点隐藏
     */
    fun setPosterItemNewVisibility(visibility: Int) {
        posterViewSection.setItemNewVisibility(visibility)
    }

}